package com.gerfmarquez.nymph.ui.seekbar;

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.widget.SeekBar;

import com.gerfmarquez.nymph.ui.gesture.detectors.YScrollDetector;

public class VerticalSeekBar extends SeekBar  {
	
	private static String TAG = "VerticalSeekBar";
	GestureDetector gestureDetectorYScroll;
//	private int idVerticalSeekBar;


	public VerticalSeekBar(Context context) {
		super(context);
		gestureDetectorYScroll = new GestureDetector(new YScrollDetector());
    }

    public VerticalSeekBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        
        gestureDetectorYScroll = new GestureDetector(new YScrollDetector());
    }

    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(h, w, oldh, oldw);
    }

    @Override
    protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(heightMeasureSpec, widthMeasureSpec);
        setMeasuredDimension(getMeasuredHeight(), getMeasuredWidth());
    }

    protected void onDraw(Canvas c) {
        c.rotate(-90);
        c.translate(-getHeight(), 0);

        super.onDraw(c);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
//    	Log.d(TAG, "onTouchEvent:::::::::::::::::::::::::"+event.getAction());
        if (!isEnabled()) {
            return false;
        }
		if(gestureDetectorYScroll.onTouchEvent(event)) {
	        switch (event.getAction()) {
		        case MotionEvent.ACTION_DOWN:
	            case MotionEvent.ACTION_MOVE:
	            case MotionEvent.ACTION_UP:
	            	int musicNotesLevels = getMax()/11;
	            	int progress = getMax() -(int) (getMax() * event.getY() / getHeight());
	            	int noteNumber = ((progress/musicNotesLevels)+1);
	            	if(progress%musicNotesLevels < 1) {
	            		Log.d(TAG,"Progress = "+progress+" musicNotesLevels = "+musicNotesLevels);
	            		if(noteNumber > 0 && noteNumber < 13 ) {
	            			Log.d(TAG,"Note number = "+noteNumber);
	            			setProgress(progress);
	            			onSizeChanged(getWidth(), getHeight(), 0, 0);
	            		}
	            	}
//	            	Log.d(TAG,"Max = "+getMax()+" Y = "+event.getY()+"  X = "+event.getX()+" Height = "+getHeight());
//	            	Log.d(TAG,"Current Progress = "+getProgress());
	                break;
	            case MotionEvent.ACTION_CANCEL:
	        }
		}
        return true;
    }


//    public int getIdVerticalSeekBar() {
//    	return idVerticalSeekBar;
//    }
//    
//    public void setIdVerticalSeekBar(int idVerticalSeekBar) {
//    	this.idVerticalSeekBar = idVerticalSeekBar;
//    }

    
}
